# -*- coding: utf-8 -*-

#   Copyright (C) 2011  P.L. Lucas, P.L. del Castillo
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

import pygtk
pygtk.require('2.0')
import gtk
import cairo
import pangocairo
import os
import Image
import ImageFilter
from array import array 
from config import *

def image_resize_pixbuf_width_height( pixbuf, width, height):
	if pixbuf.get_width()==width and pixbuf.get_height()==height:
		return width, height
	w=width
	h=float(width)*float(pixbuf.get_height())/float(pixbuf.get_width())
	if pixbuf.get_width()<pixbuf.get_height():
		w=float(height)*float(pixbuf.get_width())/float(pixbuf.get_height())
		h=height
	return int(w+0.5), int(h+0.5)

def image_resize_pixbuf_width_height_odd_even( pixbuf, width, height):
	if pixbuf.get_width()==width and pixbuf.get_height()==height:
		return width, height
	w=width
	h=float(width)*float(pixbuf.get_height())/float(pixbuf.get_width())
	if pixbuf.get_width()<pixbuf.get_height():
		w=float(height)*float(pixbuf.get_width())/float(pixbuf.get_height())
		h=height
	w, h=int(w+0.5), int(h+0.5)
	if w%2==0:
		w+=1
	if h%2==0:
		h+=1
	factor=abs(float(w)/float(h) - float(pixbuf.get_width())/float(pixbuf.get_height()))
	factor_h=abs(float(w)/float(h-2) - float(pixbuf.get_width())/float(pixbuf.get_height()))
	factor_w=abs(float(w-2)/float(h) - float(pixbuf.get_width())/float(pixbuf.get_height()))
	if factor > factor_h and factor_w > factor_h:
		h-=2
	elif factor > factor_w and factor_h > factor_w:
		w-=2
	return w, h

def image_resize_pixbuf_to_size( pixbuf, width, height):
	w, h = image_resize_pixbuf_width_height( pixbuf, width, height)
	pixmap=gtk.gdk.Pixmap(None, w, h, 24)
	ct=pixmap.cairo_create()
	pix=pixbuf.scale_simple(w, h, gtk.gdk.INTERP_BILINEAR)
	ct.set_source_pixbuf(pix,0,0)
	ct.paint()
	pix=gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, width, height)
	pix=pix.get_from_drawable(pixmap, pixmap.get_colormap(), 0,0,0,0, width, height)
	return pix


def image_adjust_pixbuf_to_size( pixbuf, width, height):
	if pixbuf.get_width()==width and pixbuf.get_height()==height:
		return pixbuf
	pixmap=gtk.gdk.Pixmap(None, width, height, 24)
	ct=pixmap.cairo_create()
	ct.set_source_rgb(0, 0, 0)
	ct.rectangle(0, 0, width, height)
	ct.fill()
	w, h = image_resize_pixbuf_width_height( pixbuf, width, height)
	pix=pixbuf.scale_simple(w,h,gtk.gdk.INTERP_BILINEAR)
	ct.set_source_pixbuf(pix,width/2-w/2, height/2-h/2)
	ct.paint()
	pix=gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, width, height)
	pix=pix.get_from_drawable(pixmap, pixmap.get_colormap(), 0,0,0,0, width, height)
	return pix

def imageSurface_new(width, height):
	im=cairo.ImageSurface(cairo.FORMAT_RGB24,width,height)
	ct1=cairo.Context(im)
	ct=gtk.gdk.CairoContext(pangocairo.CairoContext(ct1))
	ct.set_source_rgb(0, 0, 0)
	ct.rectangle(0, 0, width, height)
	ct.fill()
	ct.paint()
	return im, ct

def imageSurface_get_pixbuf(im):
	pixbuf=gtk.gdk.pixbuf_new_from_data(im.get_data(), gtk.gdk.COLORSPACE_RGB, True, 8, im.get_width(), im.get_height(), im.get_stride())
	im, ct=imageSurface_new(im.get_width(), im.get_height())
	ct.set_source_pixbuf(pixbuf,0,0)
	ct.paint()
	return gtk.gdk.pixbuf_new_from_data(im.get_data(), gtk.gdk.COLORSPACE_RGB, True, 8, im.get_width(), im.get_height(), im.get_stride())

def imageSurface_adjust_pixbuf_to_size( pixbuf, width, height):
	if pixbuf.get_width()==width and pixbuf.get_height()==height:
		return pixbuf
	im, ct=imageSurface_new(width, height)
	#ct.save()
	w, h = pixbuf.get_width(), pixbuf.get_height()
	ct.translate(width/2, height/2)
	ct.scale(float(width)/float(w),float(width)/float(w))
	ct.set_source_pixbuf(pixbuf,-w/2, -h/2)
	#ct.restore()
	ct.paint()
	return imageSurface_get_pixbuf(im)

def image_color(width, height, red, green, blue):
	im, ct=imageSurface_new(width, height)
	ct.set_source_rgb(red, green, blue)
	ct.rectangle(0, 0, width, height)
	ct.fill()
	ct.paint()
	return imageSurface_get_pixbuf(im)

def blur(im):
	im1 = Image.frombuffer("RGBA",( im.get_width(),im.get_height() ),im.get_data(),"raw","RGBA",0,1)

	imwidth = im.get_width()
	imheight = im.get_height()

	im1 = im1.filter(ImageFilter.BLUR)

	imgd = im1.tostring()
	a = array('B',imgd)

	stride = imwidth * 4
	surface = cairo.ImageSurface.create_for_data (a, cairo.FORMAT_ARGB32,
	imwidth, imheight, stride)

	ctx = cairo.Context(surface)

	ctx.scale (WIDTH/1.0, HEIGHT/1.0)

	pat = cairo.LinearGradient (0, 0, 0, 1)
	pat.add_color_stop_rgba (1, 0, 0, 0, 0)
	pat.add_color_stop_rgba (0.3, 1, 1, 1, 1)

	ctx.mask (pat)

	surface.write_to_png('gradientImage.png')
